package com.shizhanzhe.zxbapp.vip;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.MyPDD;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.utils.Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class MuchMoreOrderAdapter extends RecyclerView.Adapter<MuchMoreOrderAdapter.ViewHolder> {

    private Context mContext;
    private List<MyPDD.DataBeanX.DataBean> mData = new ArrayList<>();

    public MuchMoreOrderAdapter(Context context) {
        mContext = context;
    }

    public void updateData(int page,List<MyPDD.DataBeanX.DataBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page == 1){
                mData = dataSet;
            } else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_much_more_order, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final MyPDD.DataBeanX.DataBean dataBean = mData.get(position);
        Glide.with(mContext).load(dataBean.getGoods_thumbnail_url()).apply(new RequestOptions().error(R.mipmap.logo)).into(holder.orderImg);
        holder.orderTitle.setText(dataBean.getGoods_name());
        switch (dataBean.getOrder_status()){
            // -1 未支付; 0-已支付；1-已成团；2-确认收货；3-审核成功；4-审核失败（不可提现）；
            // 5-已经结算；8-非多多进宝商品（无佣金订单）
            case -1:
                holder.tvState.setText("未支付");
                break;
            case 0:
                holder.tvState.setText("已支付");
                break;
            case 1:
                holder.tvState.setText("已成团");
                break;
            case 2:
                holder.tvState.setText("确认收货");
                break;
            case 3:
                holder.tvState.setText("审核成功");
                break;
            case 4:
                holder.tvState.setText("审核失败");
                break;
            case 5:
                holder.tvState.setText("已结算");
                break;
            case 8:
                holder.tvState.setText("无佣金");
                break;
        }

        holder.tvFk.setText((double)dataBean.getOrder_amount()/100+"");//付款金额
        holder.tvYg.setText(Util.Sub((double)dataBean.getPromotion_amount()/100*(Double.parseDouble(UserBean.vipdivide)+Double.parseDouble(UserBean.divide))+""));//佣金
        holder.timeChuangjian.setText("创建："+getDateTimeFromMillisecond(dataBean.getOrder_create_time()));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.order_img)
        ImageView orderImg;
        @BindView(R.id.order_title)
        TextView orderTitle;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.tv_fk)
        TextView tvFk;
        @BindView(R.id.tv_yg)
        TextView tvYg;
        @BindView(R.id.time_chuangjian)
        TextView timeChuangjian;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    /**
     * 将毫秒转化成固定格式的时间
     * 时间格式: yyyy-MM-dd HH:mm:ss
     *
     * @param millisecond
     * @return
     */
    public static String getDateTimeFromMillisecond(long millisecond){
        System.setProperty("user.timezone", "Asia/Shanghai");
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(millisecond*1000);
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }
}
